<?php defined('SYSPATH') or die('No direct script access.');
 
class Karma_Model extends Model {

	public function giveKarma($fromID, $toID,$messageID,$karma)
	{
		$result=$this->db->query("INSERT INTO furryhu.forum_karma___new (timestamp,from_id,to_id,message_id,action) VALUES (NOW(),?,?,?,?)",$fromID,$toID,$postID,$karma);
	}

	public function getKarmaGiven($fromID = FALSE, $since = FALSE, $orderby = 'user', $descending = FALSE)
	{
		if ($fromID==FALSE) $from='%'; else $from=strtr($fromID,array('%'=>'\\%','_'=>'\\_','\\'=>'\\\\'));
		if ($since!=FALSE) $whenstring='AND `when_unixtime`>='.(time()-$since); else $whenstring='';
		switch ($orderby)
		{
			case 'pluscount':
			case 'minuscount':
				$orderstring='`'.$orderby.'` '.($descending?'DESC':'ASC').',`from` ASC';break;
			case 'user':
			default: 
				$orderstring='`from` '.($descending?'DESC':'ASC');
		}
		$result=$this->db->query("SELECT `from`,SUM(CASE WHEN `action`='+' THEN 1 ELSE 0 END) AS `pluscount`, SUM(CASE WHEN `action`='-' THEN 1 ELSE 0 END) AS `minuscount` FROM furryhu.forum_karma___new_display WHERE `from` LIKE '$from' $whenstring GROUP BY `from` ORDER BY $orderstring");
		return $result;
	}
	
	public function getKarmaReceived($toID = FALSE, $since = FALSE, $orderby = 'user', $descending = FALSE)
	{
		if ($toID==FALSE) $to='%'; else $to=strtr($toID,array('%'=>'\\%','_'=>'\\_','\\'=>'\\\\'));
		if ($since!=FALSE) $whenstring='AND `when_unixtime`>='.(time()-$since); else $whenstring='';
		$orderstring='';
		switch ($orderby)
		{
			case 'pluscount':
			case 'minuscount':
				$orderstring='`'.$orderby.'` '.($descending?'DESC':'ASC').',`to` ASC';break;
			case 'user':
			default:
				$orderstring='`to` '.($descending?'DESC':'ASC');
		}
		$result=$this->db->query("SELECT `to`,SUM(CASE WHEN `action`='+' THEN 1 ELSE 0 END) AS `pluscount`, SUM(CASE WHEN `action`='-' THEN 1 ELSE 0 END) AS `minuscount` FROM furryhu.forum_karma___new_display WHERE `to` LIKE '$to' $whenstring GROUP BY `to` ORDER BY $orderstring");
		return $result;
	}

	public function getKarmaDetailed($fromID = FALSE, $toID = FALSE, $since = FALSE, $orderby = 'from', $descending = FALSE)
	{
		if ($fromID==FALSE) $from='%'; else $from=strtr($fromID,array('%'=>'\\%','_'=>'\\_','\\'=>'\\\\'));
		if ($toID==FALSE) $to='%'; else $to=strtr($toID,array('%'=>'\\%','_'=>'\\_','\\'=>'\\\\'));
		if ($since!=FALSE) $whenstring='AND `when_unixtime`>='.(time()-$since); else $whenstring='';
		switch ($orderby)
		{
			case 'from':$orderstring='`from` '.($descending?'DESC':'ASC').',`to` '.($descending?'DESC':'ASC');break;
			case 'to':$orderstring='`to` '.($descending?'DESC':'ASC').',`from` '.($descending?'DESC':'ASC');break;
			case 'pluscount':
			case 'minuscount':
				$orderstring='`'.$orderby.'` '.($descending?'DESC':'ASC').',`from` ASC,`to` ASC';
		}
		$result=$this->db->query("SELECT `to`,`from`,SUM(CASE WHEN `action`='+' THEN 1 ELSE 0 END) AS `pluscount`, SUM(CASE WHEN `action`='-' THEN 1 ELSE 0 END) AS `minuscount` FROM furryhu.forum_karma___new_display WHERE `from` LIKE '$from' AND `to` LIKE '$to' $whenstring GROUP BY `from`,`to` ORDER BY $orderstring;");
		return $result;
	}

	public function getFromUsers()
	{
		$result=$this->db->Query('SELECT DISTINCT `from` FROM furryhu.forum_karma___display');
		$retval=array();
		foreach ($result as $row)
		{
			$retval[strtolower($row->from)]=$row->from;
		}
		return $retval;
	}
	
	public function getToUsers()
	{
		$result=$this->db->Query('SELECT DISTINCT `to` FROM furryhu.forum_karma___display');
		$retval=array();
		foreach ($result as $row)
		{
			$retval[strtolower($row->to)]=$row->to;
		}
		return $retval;
	}
	
}